﻿using System.Collections.Generic;

namespace Zive.Color {

    public class BradfordTransformFactory {

        private static readonly Dictionary<Illuminants, Dictionary<Illuminants, BradfordTransform>> _transformCache;
        static BradfordTransformFactory( ) {
            _transformCache = new Dictionary<Illuminants, Dictionary<Illuminants, BradfordTransform>>( );
        }

        private static BradfordTransform _CacheGet( Illuminants source, Illuminants destination ) {
            Dictionary<Illuminants, BradfordTransform> temp;
            if ( _transformCache.TryGetValue( source, out temp ) ) {
                BradfordTransform result;
                if ( temp.TryGetValue( destination, out result ) ) {
                    return result;
                }
            }
            return null;
        }

        private static void _CachePut( Illuminants source, Illuminants destination, BradfordTransform transform ) {
            Dictionary<Illuminants, BradfordTransform> temp;
            if ( !_transformCache.TryGetValue( source, out temp ) ) {
                temp = new Dictionary<Illuminants, BradfordTransform>( );
                _transformCache[ source ] = temp;
            }
            temp.Add( destination, transform );
        }

        public static BradfordTransform Create( Illuminants source, Illuminants destination ) {
            var result = _CacheGet( source, destination );
            if ( null != result ) {
                return result;
            }

            result = new BradfordTransform( source, destination );
            _CachePut( source, destination, result );
            return result;
        }

    }

}
